//
//  CustomPickerView.m
//  WilliePikerView
//
//  Created by wei wang on 2017/9/12.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "CustomPickerView.h"

static const NSInteger nuberOfComponent = 1; //行数
static const NSInteger rowHeight = 64; //行数

@interface CustomPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

/** slectIndex **/
@property(nonatomic,assign) NSInteger slectIndex;

@end

@implementation CustomPickerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initWithFrameSize];

    }
    
    return self;
}

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        [self masLayoutSubView];

    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initWithFrameSize];
    }
    return self;
}

- (void)masLayoutSubView
{
    __weak UIView *superView = self;
    [self.customPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superView);
    }];
    
    [self.shadeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(superView);
        make.height.mas_equalTo(rowHeight);
        make.width.mas_equalTo(self.mas_width);
    }];
    
    [self sendSubviewToBack:self.shadeView];
    
    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(superView.mas_centerX).with.offset(px1080Width(150));
        make.centerY.mas_equalTo(self.shadeView.mas_centerY);
    }];
}

- (void)initWithFrameSize{
    
    __weak UIView *superView = self;
    self.customPickerView.frame = superView.frame;
    self.shadeView.frame = CGRectMake(0, 0, self.customPickerView.frame.size.width, rowHeight);
    self.shadeView.center = self.customPickerView.center;
    [self sendSubviewToBack:self.shadeView];
}


- (UIPickerView *)customPickerView{
    
    if (!_customPickerView) {
        
        __weak UIView *superView = self;
        _customPickerView = [[UIPickerView alloc] init];
        _customPickerView.delegate = self;
        _customPickerView.dataSource = self;
        _customPickerView.backgroundColor = [UIColor clearColor];
        [superView addSubview:_customPickerView];
        
    }
    
    return _customPickerView;
}

- (UIView *)shadeView{
    
    if (!_shadeView) {
        
        __weak UIView *superView = self;
        _shadeView = [[UIView alloc] init];
//        _shadeView.backgroundColor = rgba(88,111,161,1);
        [superView addSubview:_shadeView];
        
    }
    
    return _shadeView;
}

- (UILabel *)placeLabel{
    
    if (!_placeLabel) {
        
        __weak UIView *superView = self.shadeView;
        _placeLabel = [[UILabel alloc] init];
        [_placeLabel setTextAlignment:NSTextAlignmentLeft];
        [_placeLabel setFont:[UIFont boldSystemFontOfSize:14]];
        _placeLabel.textColor = [UIColor whiteColor];
//        _placeLabel.text = @"步";
        [superView addSubview:_placeLabel];
        
    }
    return _placeLabel;
}

- (NSMutableArray *)datasource{
    
    if (!_datasource) {
        
        _datasource = [NSMutableArray array];
        
    }
    return _datasource;
}

#pragma mark - UIPickerViewDataSource

// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return nuberOfComponent;
}

// 返回每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return self.datasource.count;
}

#pragma mark - UIPickerViewDelegate

// 反回pickerView的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED{
 
    return self.frame.size.width;
}

// 返回pickerView的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED{
    
    return rowHeight;
}

// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return [NSString stringWithFormat:@"%@",self.datasource[row]];
}

// 选中行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED{
    
    self.slectIndex = row;

    [pickerView reloadComponent:component];
    if (self.customPickerViewDelegate != nil && [self.customPickerViewDelegate respondsToSelector:@selector(customPickerView:didSelectRow:inComponent:)]) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.customPickerViewDelegate customPickerView:pickerView didSelectRow:row inComponent:component];
        });
        
    }

}

//重写方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:30]];
        pickerLabel.textColor = [UIColor whiteColor];
        pickerView.backgroundColor = [UIColor clearColor];
        
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    CGSize size = [pickerLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]}];


    // Fill the label text here
//    if (row == self.slectIndex) {
//        
//        pickerLabel.textColor = [UIColor whiteColor];
////        if (self.shadeView.center.x == 0)
////        {
//            self.placeLabel.frame = CGRectMake(SCREEN_WIDTH/2 + size.width/2+2 , 5, 50, rowHeight);
////        }
////        else
////        {
////            self.placeLabel.frame = CGRectMake(self.shadeView.center.x + size.width/2+2 , 5, 50, rowHeight);
////
////        }
//
//    }


    return pickerLabel;
}

- (void)showLocationByValue:(NSString *)value
{
    for (int i = 0; i < self.datasource.count; i ++) {
//        NSArray *comList = self.datasource[i];
//        for (int j = 0; j < comList.count ;j++) {
            if ([self.datasource[i] isEqualToString:value])
            {
                [self.customPickerView selectRow:i inComponent:0 animated:YES];
                self.slectIndex = i;
            }
//        }
    }
}

@end
